package com.example.reeman.ymfacedemo;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.example.reeman.ymfacedemo.readface.FloatWindowManager;
import com.example.reeman.ymfacedemo.readface.ui.FaceRecoActivity;
import com.example.reeman.ymfacedemo.readface.ui.icount.ManageFaceActivity;
import com.example.reeman.ymfacedemo.utils.CameraUtils;

public class MainActivity extends AppCompatActivity {

    private MyReceiver myReceiver;
    private TextView name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        name = (TextView) findViewById(R.id.name);
        myReceiver = new MyReceiver();
        IntentFilter filter = new IntentFilter("COM.MELON.RESULT");
        registerReceiver(myReceiver, filter);
    }

    @Override
    protected void onResume() {
        super.onResume();
        //显示人脸识别
        if (CameraUtils.checkUsb()) {
            FloatWindowManager.createReadfaceWindow(this);//readface
        } else {
            Toast.makeText(this,"usb摄像头未挂载",Toast.LENGTH_LONG).show();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        FloatWindowManager.removeAllView(this);
    }

    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.face_add) {
            startActivityForResult(new Intent(this, ManageFaceActivity.class), 100);
        } else if (id==R.id.face_reco){
            startActivity(new Intent(this, FaceRecoActivity.class));
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(myReceiver);
    }

    public class MyReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            String namme = intent.getStringExtra("name");
            name.setText(namme);
        }
    }
}
